<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\OpenApiConfig;
// ======== updc ========
use updc\bean\Algorithm;
use updc\bean\GateWayRequest;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        
        // echo "<pre>";
        $sndDt = date("YmdHis");
            
        // 	// C扫B（扫码支付）
        // 	// 该 demo 是之前联调的商户提供的，后续的商户也都是参照这份 demo 成功完成联调
        // 	// 其它接口参照下面的函数完成
        // 	// 无论聚合还是条码，接口的格式都是一样的，只是 methodName、version和 msgBody 不同，对照文档上送即可
        // 	public function  testCsancB() {

		// 测试订单号
		// $merOrdrNo = "898440160510003". date("Ymd") . "093637002"; // 商户订单号
		// // 89844016051000320200909093637001
		// //数据传输主体,merOrdrNo 包含日期数据，服务端会进行校验，所以调试时要获取最新的数据，具体看接口文档描述
		// // sndDt 需上送当下时间
		// $bizContentJson='{"sndDt":"'.$sndDt.'","busiMerNo":"898440160510003","msgBody":{"trxTtlAmt":"1","trxCurr":"CNY","subject":"条码支付","ordrDesc":"支付","oprId":"9527XX","trxTrmNo":"1000001","trxTrmIp":"127.0.0.1","timeoutExpress":"5m","buyerInf":{"bankCode":"00000000","acctType":"0","limitCreditPay":"0"},"sceneInf":{"storeId":"10006","storeNm":"一间小店","areaCode":"5810","address":"广州"},"goodsDtl":[{"gdsId":"1001","gdsPlfId":"10009","gdsNm":"一件商品","gdsCnt":"1","gdsPrice":"1000","gdsCstPrice":"1000","gdsCtgy":"10010","gdsDesc":"商品描述","showUrl":"127.0.0.1"}],"trxType":"NATIVE","trxTrmInf":{"trxTrmType":"08","trxTrmNo":"10086","trxTrmIp":"10.10.0.1","trxTrmMac":"F0E1D2C3B4A5","trxTrmImei":"10002","trxTrmImsi":"10003","trxTrmIccid":"10001","trxTrmWifiMac":"10004"},"rskInf":{"devId":"10000","devcMode":"设备型号XXX","devcLanguage":"JAV","devcLocation":"116.360207","devcNumber":"10086","devcSimNumber":"2"},"merOrdrNo":"'. $merOrdrNo . '","bizFunc":"111011","busiId":"00250005","remark":"remark","remark1":"remark1","remark2":"remark2","remark3":"remark3","remark4":""},"notifyUrl":"http://192.168.180.116:8080/gnete-upbc-merdemo/resultNotifyServlet","remark":"商户联调测试"}';

		// 动态生成订单号
		echo date("His",time()) . str_pad(strval(mt_rand(0, 999)), 3, "0", STR_PAD_BOTH) . '\n';

		// 生产订单号
		$merOrdrNo = "898350308989301". date("Ymd") . date("His",time()) . str_pad(strval(mt_rand(0, 999)), 3, "0", STR_PAD_BOTH); // 商户订单号
		// 89835030898930120231009093637001
		$bizContentJson='{"sndDt":"'.$sndDt.'","busiMerNo":"898350308989301","msgBody":{"trxTtlAmt":"1","trxCurr":"CNY","subject":"条码支付","ordrDesc":"支付","oprId":"9527XX","trxTrmNo":"1000001","trxTrmIp":"127.0.0.1","timeoutExpress":"5m","buyerInf":{"bankCode":"00000000","acctType":"0","limitCreditPay":"0"},"sceneInf":{"storeId":"10006","storeNm":"一间小店","areaCode":"5810","address":"广州"},"goodsDtl":[{"gdsId":"1001","gdsPlfId":"10009","gdsNm":"一件商品","gdsCnt":"1","gdsPrice":"1000","gdsCstPrice":"1000","gdsCtgy":"10010","gdsDesc":"商品描述","showUrl":"127.0.0.1"}],"trxType":"NATIVE","trxTrmInf":{"trxTrmType":"08","trxTrmNo":"10086","trxTrmIp":"10.10.0.1","trxTrmMac":"F0E1D2C3B4A5","trxTrmImei":"10002","trxTrmImsi":"10003","trxTrmIccid":"10001","trxTrmWifiMac":"10004"},"rskInf":{"devId":"10000","devcMode":"设备型号XXX","devcLanguage":"JAV","devcLocation":"116.360207","devcNumber":"10086","devcSimNumber":"2"},"merOrdrNo":"'. $merOrdrNo . '","bizFunc":"111011","busiId":"00250029","remark":"remark","remark1":"remark1","remark2":"remark2","remark3":"remark3","remark4":""},"notifyUrl":"http://192.168.180.116:8080/gnete-upbc-merdemo/resultNotifyServlet","remark":"商户联调测试"}';




		// echo $bizContentJson . "\n";exit();

		//请求客户端对象
		$client=OpenApiConfig::getClient();
		//设置加密算法类型
		$client->__set("algorithm",Algorithm::SHA256withRSA);

		//设置公共请求参数对象
		$gateWayRequest=new GateWayRequest();
		//服务名
		$gateWayRequest->__set("apiInterfaceId","gnete.upbc.code.trade");
		//方法名
		$gateWayRequest->__set("methodName","precreate");
		//版本号
		$gateWayRequest->__set("version","1.0.1");
		$gateWayRequest->__set("bizContent",$bizContentJson);

		$client->__set("gateWayRequest",$gateWayRequest);

		 $flag=$client->apiRequest();
		 if($flag==1){
		 	echo "请求发送成功，响应验签成功\n";
		 }else{
		 	echo "请求发送成功，响应验签失败\n";
		 }

		 echo "请求主体：\n";
		 var_dump($client->__get("requestBody")) . '\n<br/><br/>';

		 echo "响应主体：\n";
		 var_dump($client->__get("responseBody"));
         echo "<br/><br/>";
         print_r($client->__get("responseBody"));

    }


    /**
     * 订单查询
     */
    public function queryOrder()
    {
		$oriTrxDt = $this->request->get("oriTrxDt");
		$oriMerOrdrNo = $this->request->get("oriMerOrdrNo");

		if(empty($oriTrxDt) || empty($oriMerOrdrNo)) {
			echo "oriTrxDt or oriMerOrdrNo is empty";
			exit();
		}
        
		// echo "<pre>";
		$sndDt = date("YmdHis");
	
		// 	// C扫B（扫码支付）
		// 	// 该 demo 是之前联调的商户提供的，后续的商户也都是参照这份 demo 成功完成联调
		// 	// 其它接口参照下面的函数完成
		// 	// 无论聚合还是条码，接口的格式都是一样的，只是 methodName、version和 msgBody 不同，对照文档上送即可
		// 	public function  testCsancB() {

		// 测试订单号
		// $merOrdrNo = "898440160510003". date("Ymd") . "093637002"; // 商户订单号
		// // 89844016051000320200909093637001
		// //数据传输主体,merOrdrNo 包含日期数据，服务端会进行校验，所以调试时要获取最新的数据，具体看接口文档描述
		// // sndDt 需上送当下时间
		// $bizContentJson='{"sndDt":"'.$sndDt.'","busiMerNo":"898440160510003","msgBody":{"trxTtlAmt":"1","trxCurr":"CNY","subject":"条码支付","ordrDesc":"支付","oprId":"9527XX","trxTrmNo":"1000001","trxTrmIp":"127.0.0.1","timeoutExpress":"5m","buyerInf":{"bankCode":"00000000","acctType":"0","limitCreditPay":"0"},"sceneInf":{"storeId":"10006","storeNm":"一间小店","areaCode":"5810","address":"广州"},"goodsDtl":[{"gdsId":"1001","gdsPlfId":"10009","gdsNm":"一件商品","gdsCnt":"1","gdsPrice":"1000","gdsCstPrice":"1000","gdsCtgy":"10010","gdsDesc":"商品描述","showUrl":"127.0.0.1"}],"trxType":"NATIVE","trxTrmInf":{"trxTrmType":"08","trxTrmNo":"10086","trxTrmIp":"10.10.0.1","trxTrmMac":"F0E1D2C3B4A5","trxTrmImei":"10002","trxTrmImsi":"10003","trxTrmIccid":"10001","trxTrmWifiMac":"10004"},"rskInf":{"devId":"10000","devcMode":"设备型号XXX","devcLanguage":"JAV","devcLocation":"116.360207","devcNumber":"10086","devcSimNumber":"2"},"merOrdrNo":"'. $merOrdrNo . '","bizFunc":"111011","busiId":"00250005","remark":"remark","remark1":"remark1","remark2":"remark2","remark3":"remark3","remark4":""},"notifyUrl":"http://192.168.180.116:8080/gnete-upbc-merdemo/resultNotifyServlet","remark":"商户联调测试"}';


		// 动态生成订单号
		echo date("His",time()) . str_pad(strval(mt_rand(0, 999)), 3, "0", STR_PAD_BOTH) . '\n';
		// echo mt_rand(0, 999);
		// str_pad(strval(mt_rand(0, 999999)), 3, "0", STR_PAD_BOTH);
		// echo time();

		// echo mt_rand(0, 999999);

		// echo str_pad(strval(mt_rand(0, 999)), 3, "0", STR_PAD_BOTH);

		// exit();

		// 生产订单号
		// $merOrdrNo = "898350308989301". date("Ymd") . "093637001"; // 商户订单号
		$merOrdrNo = "898350308989301". date("Ymd") . date("His",time()) . str_pad(strval(mt_rand(0, 999)), 3, "0", STR_PAD_BOTH);
		// 89835030898930120231009093637001
		$bizContentJson='{"sndDt":"' . $sndDt . '","busiMerNo":"898350308989301","msgBody":{"merOrdrNo":"' . $merOrdrNo . '","oriMerOrdrNo":"' . $oriMerOrdrNo . '","oriTrxDt":"' . $oriTrxDt . '","bizFunc":"611011","remark":"xxx","remark1":"xxx","remark2":"xxx"}}';


		//请求客户端对象
		$client=OpenApiConfig::getClient();
		//设置加密算法类型
		$client->__set("algorithm",Algorithm::SHA256withRSA);

		//设置公共请求参数对象
		$gateWayRequest=new GateWayRequest();
		//服务名
		$gateWayRequest->__set("apiInterfaceId","gnete.upbc.cashier.trade");
		//方法名
		$gateWayRequest->__set("methodName","payQuery");
		//版本号
		$gateWayRequest->__set("version","1.0.1");
		$gateWayRequest->__set("bizContent",$bizContentJson);

		$client->__set("gateWayRequest",$gateWayRequest);

		 $flag=$client->apiRequest();
		 if($flag==1){
		 	echo "请求发送成功，响应验签成功\n";
		 }else{
		 	echo "请求发送成功，响应验签失败\n";
		 }

		 echo "请求主体：\n";
		 var_dump($client->__get("requestBody"));

		 echo "响应主体：\n";
		 var_dump($client->__get("responseBody"));		

         echo "<br/><br/>";
         print_r($client->__get("responseBody"));
        
    }

    /**
     * 退款
     * 
     */
     public function refund()
     {  


     }


     /**
      * 退款查询

      */
      public function refundQuery()
      { 

      }


      /**
       * 订单关闭
       * 
       */
      public function closeOrder()
      {

      }

    


}
